#ifndef DEVICE_MANAGER_H
#define DEVICE_MANAGER_H

#include "common.h"

// 设备节点结构
typedef struct device_node {
    char *name;
    char *plugin;
    struct device_node *child;
    struct device_node *next;
} device_node_t;

// 设备标签结构
typedef struct device_tag {
    int type;
    char *name;
    int attribute;
    int precision;
    int decimal;
    char *unit;
    char *address;
    char *description;
    json_t *value;
    char *labparm;
    char *parmname;
    int algorithm;
    char *tagjson;
    time_t last_update;
} device_tag_t;

// 设备组结构
typedef struct device_group {
    char *node_name;
    char *group_name;
    device_tag_t **tags;
    size_t tag_count;
    time_t last_sync;
    struct device_group *next;
} device_group_t;

// 设备管理器
typedef struct {
    char *base_url;
    char *auth_token;
    device_node_t *device_tree;
    device_group_t *groups;
    pthread_rwlock_t rwlock;
    event_loop_t *event_loop;
    timer_manager_t *timer;
    data_pool_t *data_pool;
    bool initialized;
} device_manager_t;

// 实时数据读取请求
typedef struct {
    char *node;
    char *group;
} read_request_t;

// 实时数据响应
typedef struct {
    char *node;
    char *group;
    json_t *tag_values; // {"tag_name": value, ...}
    time_t timestamp;
} read_response_t;

// 设备控制请求
typedef struct {
    char *node;
    char *group;
    char *tag;
    json_t *value;
} control_request_t;

// 设备控制响应
typedef struct {
    int error;
    char *message;
} control_response_t;

// 设备管理器API
int device_manager_init(device_manager_t *manager, const char *base_url, 
                       event_loop_t *event_loop, timer_manager_t *timer, 
                       data_pool_t *data_pool);
int device_manager_set_auth(device_manager_t *manager, const char *auth_token);

// 设备发现和同步
int device_manager_discover_devices(device_manager_t *manager);
int device_manager_sync_group_tags(device_manager_t *manager, const char *node, const char *group);
int device_manager_sync_all_groups(device_manager_t *manager);

// 数据读取
read_response_t *device_manager_read_group(device_manager_t *manager, const char *node, const char *group);
int device_manager_start_polling(device_manager_t *manager, int interval_ms);
int device_manager_stop_polling(device_manager_t *manager);

// 设备控制
control_response_t *device_manager_write_tag(device_manager_t *manager, control_request_t *request);
int device_manager_write_tag_async(device_manager_t *manager, control_request_t *request, 
                                  void (*callback)(control_response_t *, void *), void *user_data);

// 查询功能
device_node_t *device_manager_get_device_tree(device_manager_t *manager);
device_group_t *device_manager_find_group(device_manager_t *manager, const char *node, const char *group);
device_tag_t *device_manager_find_tag(device_manager_t *manager, const char *node, const char *group, const char *tag);

// 工具函数
void device_manager_cleanup(device_manager_t *manager);
char *device_data_to_data_pool_key(const char *node, const char *group, const char *tag_name);

#endif
